package org.filetransfer.file_transfer_java_backend.entity;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.Instant;
import java.time.LocalDateTime;
import java.util.List;
@Entity
@Table(name = "share_item")
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class ShareItem {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    // 分享码
    @Column(name = "code", unique = true, nullable = false)
    private String code;
    // 分享创建时间
    @Column(name = "create_time")
    private Instant createTime;
    // 分享过期时间，可空
    @Schema(description = "分享过期时间，可空", nullable = true)
    @Column(name = "expiry_time")
    private Instant expiryTime;    @Column(name = "edit_token")
    // 分享完成后由后端生成
    // 用来编辑分享时需要带上这个token
    @JsonIgnore// 直接返回时忽略此字段 不会泄露
    private String editToken;
    
    // 分享创建者
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "user_id")
    @JsonIgnore
    private User user;
    
    @OneToMany(mappedBy = "shareItem", cascade = CascadeType.ALL)
    private List<File> files;
    @OneToMany(mappedBy = "shareItem", cascade = CascadeType.ALL)
    private List<Message> messages;
    @PrePersist
    protected void onCreate() {// 自动创建时间
        this.createTime = Instant.now();
    }
}